package com.news.news.config;

import com.news.news.util.TokenInterceptor;
import com.news.news.util.UserTokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

@Configuration
public class TokenConfig implements WebMvcConfigurer{

    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration inter =  registry.addInterceptor(new TokenInterceptor());
        inter.addPathPatterns("/admin/**"); //管理员需要拦截过滤地址
        inter.excludePathPatterns("/admin/loginCtl/login");//放行地址

        InterceptorRegistration userinter =  registry.addInterceptor(new UserTokenInterceptor());
        userinter.addPathPatterns("/userApi/**"); //管理员需要拦截过滤地址
        userinter.addPathPatterns("/newsDetail/**"); //管理员需要拦截过滤地址
        userinter.excludePathPatterns("/userApi/UsersLoginCtl/login");//放行地址
        userinter.excludePathPatterns("/userApi/userCtl/*");//放行地址
        userinter.excludePathPatterns("/newsDetail/newsDetailCtl/findNewsByid"); //放行地址
        userinter.excludePathPatterns("/newsDetail/newsDetailCtl/findlikeCollectionNum"); //放行地址
        userinter.excludePathPatterns("/newsDetail/newsDetailCtl/findComments"); //放行地址
        userinter.excludePathPatterns("/userApi/UsersLoginCtl/findNewsTypeList"); //放行地址




        //放行行前台首页,文章详细信息等地址
        inter.excludePathPatterns("/swagger*/**"); //放行swagger
        inter.excludePathPatterns("/v2/**");//放行swagger
        inter.excludePathPatterns("/webjars/**");//放行swagger
    }

    /**
     * web层DispatcherServlet配置 / ,所有访问后端的请求都会进入DispatcherServlet，swagger组件生成的api是网页格式的、是在后端生成的，前端势必就会访问swagger的html页面，而被DispatcherServlet拦截 。所以需要进行配置访问swagger相关页面时，不让其进入到DispatcherServlet
     * @param registry
     */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("swagger-ui.html").addResourceLocations(
				"classpath:/META-INF/resources/");
		registry.addResourceHandler("/webjars/**").addResourceLocations(
				"classpath:/META-INF/resources/webjars/");
	}
}
